package com.yc.boot.work.business;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.boot.common.model.base.BaseEntity;
import com.yc.boot.common.model.response.Outcome;
import com.yc.boot.sys.entity.SysDictionaries;
import com.yc.boot.work.entity.Test;
import com.yc.boot.work.entity.Testzmz;
import com.yc.boot.work.model.find.TestPage;
import com.yc.boot.work.model.save.SaveTest;
import com.yc.boot.work.model.update.UpdateTest;
import com.yc.boot.work.service.TestzmzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.yc.boot.common.constant.BaseConstant.MYSQL_LIMIT_ONE;


/**
 * Create with IntelliT IDEA
 *
 * @Author: zhengmingzhe
 * @Date: 2021/12/08/15:26
 * @Description:
 */
@Slf4j
@Service
public class TestzmzBussiness {
    @Resource
    private TestzmzService testzmzService;
    /*
     * @Description 查询所有信息
     * @Param 
     * @return List<Testzmz>
     **/    
    public List<Testzmz> queryAll(){
        return  testzmzService.list();

    }
    /*
     * @Description 根据Id查询一条test信息
     * @Param   Long param
     * @return object
     **/
    public Testzmz getById(Long param){

        return testzmzService.getById(param);
    }

    /*
     * @Description  根据名称进行模糊查询
     * @Param String name
     * @return
     **/
    public List<Testzmz> getByName(String name){
        List<Testzmz> zmztest = testzmzService.list(new QueryWrapper<Testzmz>()
                       .lambda().like(StrUtil.isNotBlank(name),Testzmz::getName, name));
        return zmztest;
    }

    /*
     * @Description 根据名字精准查询
     * @Param   String name
     * @return   Testzmz
     **/
    public Testzmz getByNameOccu(String name){
        Testzmz zmztest =testzmzService.getOne(
                new QueryWrapper<Testzmz>().lambda().eq(StrUtil.isNotBlank(name),Testzmz::getName,name).last(MYSQL_LIMIT_ONE)
        );
        return zmztest;
    }
    /*
     * @Description 根据创建日期范围查询
     * @Param
     * @return
     **/
// Convert 一般在修改删除插入的时候用到，比如插入最后插入的是基础实体类的字段得用这个把条件实体类转换不补全
// 条件实体类可以是对基础实体类的属性进行拓展用于条件查询基础实体类里面不一定有相应的属性，
// ge(Testzmz::getCreateTime,date.getCreateTime()) 前面的参数是对数据库字段映射 后面的参数来自于
// 条件实体类要与前面的类型保持一致 ，条件构造器里面的是基础实体类的类型
    public List<Testzmz> getByTime(TestPage date){
        List<Testzmz> zmztest =testzmzService.list(new QueryWrapper<Testzmz>()
                .lambda().ge(ObjectUtil.isNotNull(date.getCreateTime()),Testzmz::getCreateTime,date.getCreateTime())
                .le(ObjectUtil.isNotNull(date.getUpdateTime()),Testzmz::getUpdateTime,date.getUpdateTime())
                .orderByDesc(Testzmz::getCreateTime)
        );
        return zmztest;
    }

    /*
     * @Description   分页查询
     * @Param    TestPage param
     * @return IPage<Testzmz>
     **/
//   /*参数实体类只需要给数据 想怎么用就怎么用只要在查询的时候保证数据类型能对的上要查询的数据类型*/
    public IPage<Testzmz> queryByPage(TestPage param){
        IPage<Testzmz> testpage =testzmzService.page(
                new Page<>(param.getCurrent(),param.getSize()),
                new QueryWrapper<Testzmz>().lambda()
                .eq(ObjectUtil.isNotNull(param.getId()),Testzmz::getId,param.getId())
                .like(ObjectUtil.isNotNull(param.getName()),Testzmz::getName,param.getName())
                .le(ObjectUtil.isNotNull(param.getCreateTimeEnd()), Testzmz::getCreateTime, param.getCreateTimeEnd())
                .ge(ObjectUtil.isNotNull(param.getCreateTimeStart()), Testzmz::getCreateTime, param.getCreateTimeStart()
                )
        );

        return testpage;

    }

    /*
     * @Description 对Testzmz表插入单条记录
     * @Param   Testzmz data
     * @return Boolean
     **/
    public Testzmz save(SaveTest data){
          Testzmz testdata=Convert.convert(Testzmz.class,data);
        if(ObjectUtil.isNull(testdata.getCreateBy())){
            testdata.setCreateBy(1L);
        }
        Boolean savestatus = testzmzService.save(testdata);
        Testzmz currentdata =null;
        try {
            if(savestatus ==true){
                currentdata =getByNameOccu(testdata.getName());
            }
            else {
                currentdata =null;
            }
        }catch(Exception e){
          log.info("插入的数据有误");
        }
        finally {
            return currentdata;
        }

    }

    /*
     * @Description 默认一次插入两条数据到testzmz表
     * @Param  List<Testzmz>
     * @return   Boolean
     **/
    public Boolean saveBatch(List<SaveTest> data ,Long siz){
        if(ObjectUtil.isNotNull(data)) {
            List<Testzmz> testdata = new ArrayList<Testzmz>();
            for (SaveTest i : data) {
                Testzmz test = Convert.convert(Testzmz.class, i);
                if(ObjectUtil.isNull(test.getCreateBy())){
                    test.setCreateBy(1l);
                }
                if(ObjectUtil.isEmpty(test.getStatus())){
                    test.setStatus("2");
                }
                if(ObjectUtil.hasEmpty(test.getName())){
                    test.setName("默认名称");
                }
                testdata.add(test);
            }
            Integer size = Convert.convert(Integer.class, siz);
            return testzmzService.saveBatch(testdata, size);
        }
        else{
            return false;
        }
    }

        /*
         * @Description 根据id进行修改
         * @Param  UpdateTest
         * @return  Boolean
         **/
        public Boolean updateById(UpdateTest updateparam){
            Testzmz updatetest =Convert.convert(Testzmz.class,updateparam);
            return testzmzService.updateById(updatetest);
        }


        /*
         * @Description 根据多个Id进行批量修改
         * @Param
         * @reture
         **/

        public Boolean updateBatchById(List<UpdateTest> updateid){

                if(ObjectUtil.isNotNull(updateid)) {
                    boolean updateBatchById = testzmzService.updateBatchById(Convert.toList(Testzmz.class, updateid.stream()
                            .map(i -> new Testzmz().setName(i.getName()).setStatus(i.getStatus()).setUpdateBy(1l).setId(i.getId())).collect(Collectors.toList())));
                 return  updateBatchById;
                }
               return false;
        }


    /*
     * @Description 根据多个Id进行批量修改
     * @Param
     * @reture
     **/
    public Boolean updateBatchByIds(List<UpdateTest> updateid){

        if(ObjectUtil.isNotNull(updateid)) {

            boolean updateBatchById = testzmzService.updateBatchById(Convert.toList(Testzmz.class, updateid));
            return  updateBatchById;
        }
        return false;
    }

    /*
     * @Description 根据Id进行删除
     * @Param Testzmz param
     * @return Boolean
     **/

    public Boolean removeById(Testzmz param){

        return testzmzService.removeById(param);
    }

    /*
     * @Description 根据多个ID进行批量删除
     * @Param
     * @return
     **/
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeBatchByIds(List<Long> id){
        if(ObjectUtil.isNotNull(id)){
            boolean updateBatchById = testzmzService.removeByIds(id);
            return  updateBatchById;
        }
        return false;
    }
}
